<?php 

namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use Auth,DB;
use App\Articles;

class AdminController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin/home');
    }

    public function userList()
    {
        $users = DB::table('users')->get();
        return view()->make('admin.user.list')->with(['users'=>$users]);
    }

    public function userDelete(Request $request)
    {
        $id = $request->input('id');
        $rs = DB::table('users')->where('id', $id)->delete();
        if($rs){
            $notice = "删除成功";
        }else{
            $notice = "删除失败";
        }
        #Todo show the $notice on view
        return redirect(route('usermanage.list'))->with(['notice'=>$notice]);
    }

    public function articleList()
    {
        $articles = DB::table('articles')->get();
        return view()->make('admin.article.list')->with(['articles'=>$articles]);
    }

    public function articleDelete(Request $request)
    {
        $id = $request->input('id');
        $rs = DB::table('articles')->where('id', $id)->delete();
        if($rs){
            $notice = "删除成功";
        }else{
            $notice = "删除失败";
        }
        #Todo show the $notice on view
        return redirect(route('articlemanage.list'))->with(['notice'=>$notice]);
    }

    public function webConfig()
    {
        $config = DB::table('siteinfo')->where('id', 1)->first();
        return view()->make('admin.webconfig')->with(['config'=>$config]);
    }

    public function editWebConfig(Request $request)
    {
        DB::table('siteinfo')->where('id', 1)->update($request->except('_token'));
        return redirect(route('webconfig'));
    }
}
